//
//  anpaiViewController.m
//  shuilijuOASystem
//
//  Created by michael on 16/6/28.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "tablTableViewCell.h"
#import "anpaiViewController.h"
#import "GDataDefines.h"
#import "UnDoneModel.h"
#import "OADetailViewController.h"
#import "OADetailDisPatchController.h"
#import "OAUnDoneListCell.h"
#import "PickerChoiceView.h"
#import "TableViewCell.h"

@interface anpaiViewController ()<UITableViewDelegate,UITableViewDataSource,TFPickerDelegate>
{
    NSInteger selectIndex;
}
@property(nonatomic,strong)NSMutableArray * dataArray;
@property(nonatomic,assign)int totalCounts; //总条数
@property(nonatomic,assign)int currentPage; //当前页
@property (nonatomic,strong)NSMutableArray *mutArray;
@property (nonatomic,strong)NSMutableArray *Array;
@property (nonatomic,strong)NSMutableArray *numberArray;

@property (nonatomic,strong)NSArray *array;
@property (nonatomic,strong)NSArray *numberarray;
@property (nonatomic,strong)NSArray *weekarray;


@end

@implementation anpaiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
      [self requestdata];

    _tableview = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStylePlain];
        UIImageView *backImageView=[[UIImageView alloc]initWithFrame:self.view.frame];
       [backImageView setImage:[UIImage imageNamed:@"领导一周工作安排背景"]];
self.tableview.backgroundView = backImageView;
    _tableview.dataSource = self;
    _tableview.delegate = self;
    self.title = @"详情";
    [self.view addSubview:_tableview];
    _weekarray =  @[@"星期一",@"星期二",@"星期三",@"星期四",@"星期五",@"星期六",@"星期日"];

      // 分类搜索
    _array = _Array;
  _numberarray = _numberArray;
  UINib *nib = [UINib nibWithNibName:@"TableViewCell" bundle:nil];
    [self.tableview registerNib:nib forCellReuseIdentifier:@"TableViewCellID"];
    _method = @"contentLeader";
    _url = exchleader;
 
    
    // Do any additional setup after loading the view.
}
- (void)requestdata{
NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unid":_fileId},@{@"userId":userId}];
    [HttpTools requestWithURL:exchleader arrays:array method:@"getLeader " success:^(id JSON) {
     NSArray *array = JSON [@"data"];
     _Array = [NSMutableArray array];
     _numberArray = [NSMutableArray array];
     for (NSDictionary *dic in array) {
    NSString *leader = dic[@"leader"];
     NSString *number = dic[@"number"];
    [_Array addObject:leader];
    [_numberArray addObject:number];
}
_array = _Array;
_numberarray = _numberArray;
   self.mutArray = [NSMutableArray array];

  NSArray *arr = @[@{@"title1":@"请点击此处选择类别",@"title2":_array [0]  }];
    for (NSDictionary *dict in arr) {
        
        [self.mutArray addObject:dict];
    }

_name = _Array[0];
_number = _numberarray[0];

   [self loadData];
  
        [self.tableview reloadData];
                    } failure:^(NSError *error) {
        [self.tableview headerEndRefreshing];
        
        
        
    }];

}
- (void)loadData{
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];

NSArray  *array = @[@{@"unId":self.fileId},@{@"leader":_name},@{@"number":_number},@{@"userId":userId}];

 [HttpTools requestWithURL:self.url arrays: array method:_method success:^(id JSON) {
    
  
     
    NSArray *array = JSON [@"data"];
 NSMutableArray  *Array = [NSMutableArray array];
     for (NSDictionary *dic in array) {
    NSString *leader = dic[@"leader"];
    [Array addObject:leader];
}
_dataArray = Array;

   
   
   
                [self.tableview reloadData];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];




}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
if(indexPath.row==0)
{
return 40;
}
else{
//    UnDoneModel * model = self.dataArray[indexPath.row-1];
    NSString *string = _dataArray[indexPath.row - 1];
    CGFloat height = 0;
    CGFloat  titlesize  = [OAToolBox sizebyText:string font:13 minsWidth:KScreenWidth-100];
//    
//    NSString * desstring = [NSString stringWithFormat:@"%@",model.time];
//    CGFloat dessize = [OAToolBox sizebyText:desstring font:13 minsWidth:60];;


    height = titlesize-50  ;
  if (height <  60) {
    return 60;
}
else{
 return  height;

}
    
   }
}




#pragma mark UITableView dataScource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.row==0) {
    NSDictionary *dict = [self.mutArray objectAtIndex:0];
    
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TableViewCellID"];
                            cell.backgroundColor=[UIColor clearColor];//关键语句  

    cell.leftLb.text = dict[@"title1"];
    cell.rightLb.text = dict[@"title2"];
   return cell;
}
 else{
 
 
     static NSString *identifier=@"UITableViewCellIdentifierKey1";
    tablTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:identifier];
                            cell.backgroundColor=[UIColor clearColor];//关键语句  


if(!cell){
        cell=[[tablTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier];
                                cell.backgroundColor=[UIColor clearColor];//关键语句  

    }
     
    cell.week = _weekarray[indexPath.row - 1];
    cell.data = _dataArray[indexPath.row - 1];
    

                  return cell;


    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

if (indexPath.row == 0){
PickerChoiceView *picker = [[PickerChoiceView alloc]initWithFrame:self.view.bounds];
    selectIndex = indexPath.row;
    picker.delegate = self;
    if (indexPath.section == 0) {
        
        picker.selectLb.text = @"选择领导";
        picker.customArr = _array;
    }

    [self.view addSubview:picker];

}else{
nil;
}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count+1;
}
- (void)PickerSelectorIndixString:(NSString *)str{

    NSDictionary *dict = [self.mutArray objectAtIndex:0];
    
    NSMutableDictionary *muDict = [NSMutableDictionary dictionaryWithDictionary:dict];
    [muDict setObject:str forKey:@"title2"];
       // [muDict setObject:@"" forKey:@"title1"];
    [self.mutArray replaceObjectAtIndex:0 withObject:muDict];
//    [self.mutArray removeObjectAtIndex:selectIndex];
//    [self.mutArray insertObject:muDict atIndex:selectIndex];
_name = str;
NSInteger index = [_array indexOfObject:str];

_number = _numberarray[index];


    [self loadData];
    NSLog(@"%@,%@",str,_number);
    [self.tableview reloadData];
    
}
@end
